package demo;


/**
 * Remarks:
 * Class name :Thread5
 * Date :2023/2/13 22:36
 * Author:杨俊源
 */
public class Thread5 implements Runnable {

    private static String winner;

    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
            if (Thread.currentThread().getName().equals("兔子") && i % 10 == 0){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            //判断比赛是否结束
            boolean flag = gameOver(i);
            if (flag){
                break;
            }
            System.out.println(Thread.currentThread().getName() + "-->" + i );
        }
    }

    private boolean gameOver(int steps){
        if(winner != null){
            //已经存在胜利者了，比赛结束
            return true;
        }{
            if(steps >= 100){
                winner = Thread.currentThread().getName();
                System.out.println("winner is " + winner);
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Thread5 t = new Thread5();
        new Thread(t,"wugui").start();
        new Thread(t,"tuzi").start();
    }
}
